var gbllist = [];
var robotlist = [];
$(document).ready(function (){
	showBotsList();	
});

function showBotsList() {
	var parentID = "parkbotslist";
	var listName = "list";
	var listHeader = new Array(2);
	listHeader[0] = "robot";
	listHeader[1] = "name";

	$.ajax({
		type:"GET",
		url:"index/getallrobot/location/2",
		async:true,
		beforeSend:function loading(){
		},
		success:function(v){
			var data = window.JSON.parse(v);
			if(!data.success) {
				alert(data.message);
			} else {
				var bots = data.data;
				var len = bots.length;
				var listData = new Array(len);
				for(var i = 0; i < len;i++){
					var a = new Array(3);
					var src = "images/list/boy.gif";
					if(bots[i].gender == 2)
						src = "images/list/girl.gif";
					a[0] = bots[i].id;
					a[1] = src;
					a[2] = bots[i].name;
					listData[i] = a;
				}
				robotlist = bots;
				var grid = new TGrid(parentID, listName,listHeader, listData);
				grid.show();
			}
		},
		error:function(){
		}
	});
}
function showActivityList() {
	var parentID = "activityList";
	var listName = "activityList";
	var botId = userProfile.robot.id;
	var listHeader = new Array(1);
	listHeader[0] = " ";	
	$.ajax({
		type:"GET",
		url:"index/getallactivity/id/"+botId,
		async:true,
		beforeSend:function loading(){
		},
		success:function(v){
			var data = window.JSON.parse(v);
			if(!data.success) {
				alert(data.message);
			} else {
				var act = data.data;
				var len = act.length;
				var listData = new Array(len);				
				for(var i = 0; i < len;i++){
					var a = new Array(1);
					a[0] = act[i];
					listData[i] = a;
				}
				var grid = new TGrid(parentID, listName,listHeader, listData);
				grid.show();
			}
			$.ajax({
				type:"GET",
				url:"index/markactivity/id/"+userProfile.robot.id,
				async:true,
				beforeSend:function loading(){
				},
				success:function(v){
					
				},
				error:function(){
				}
			});
		},
		error:function(){
		}
	});
	
}
function TGrid(parentID, listName,listHeader, listData) 
{
	this.parentID = parentID;
	this.listName = listName;
	this.listData = listData;
	this.listheader = listHeader;
	this.PageNo = 0;
	this.PageRow = 10;
	this.BaseNo = 0;
	this.showNum = 1;
	this.addPageToolBar = true;
	this.recentListCount = this.listData?this.listData.length:0;
	this.getPageToolBar = TGrid_getPageToolBar;
	this.headTR = TGrid_headTR;
	this.show = TGrid_show;
	this.freshGrid = TGrid_freshGrid;
	this.extendGrid = TGrid_extendGrid;
	this.getImg = TGrid_getImg;
	gbllist[this.listName] = this;
};

function TGrid_getPageToolBar()
{
	var col = this.listheader?this.listheader.length:1;
	
	var td = "";
	var s = "";
	var len = 1;
	if(this.listData)
	{
		len = this.listData.length;
		var pages = Math.ceil(len / this.PageRow);
		len = pages;
		if(pages > 10)
			len = 10;
	}
	if ((this.PageNo == 0 && pages<=1) || this.showNum == 0)
		return "";
	var opt = "";
	if(this.PageNo != 0)
	{
		opt = "onclick = 'gbllist[\""+this.listName+"\"].freshGrid(0,\"first\")' style='cursor:pointer'";
		td +="<td><img src='images/list/page-first.gif'"+opt+"/></td>";
	}
	if(this.PageNo >= this.showNum)
	{
		opt = "onclick = 'gbllist[\""+this.listName+"\"].freshGrid(0,\"prev\")' style='cursor:pointer'";
		td +="<td><img src='images/list/page-prev.gif'"+opt+"/></td>";
	}	
	this.BaseNo = (Math.ceil((this.PageNo+1)/this.showNum)-1)*this.showNum;	
	var p = 0;
	if(this.showNum < pages)
		p = this.showNum - 1;
	else
		p = pages - 1;
			
	if(this.showNum < pages)
	{
		opt = "onclick = 'gbllist[\""+this.listName+"\"].freshGrid(0,\"next\")' style='cursor:pointer'";
		td +="<td><img src='images/list/page-next.gif'"+opt+"/></td>";
	}
	if((this.PageNo%this.showNum < p) || ((this.showNum < pages) && (p == this.PageNo%this.showNum)))
	{
		opt = "onclick = 'gbllist[\""+this.listName+"\"].freshGrid(0,\"last\")' style='cursor:pointer'";
		td +="<td><img src='images/list/page-last.gif'"+opt+"/></td>";
	}
	var tr = "<tr>"+td+"</tr>";
	var tbl = "<table cellspacing=0 cellpadding=0 border=0>"+tr+"</table>";
	var td = "<td colspan = "+col+" align=\"center\">"+tbl+"</td>";
	var tr = "<tr>"+td+"</tr>";
	return tr;
};

function TGrid_freshGrid(currentPage,action)
{
	if(action == "first")
	{		
		if(this.PageNo >= this.showNum)
		{
			this.PageNo = 0;			
		}
		else
			this.PageNo = 0;
	}
	if(action == "last")
	{
		if(this.recentListCount > 0)
		{
			if(this.PageRow > 0)
				this.PageNo = Math.ceil(this.recentListCount / this.PageRow) - 1;			
		}				
	}
	if(action == "prev" || action == "next")
	{		
		this.PageNo = (action=="prev"?(this.BaseNo-1):(this.BaseNo+this.showNum));
		if(this.PageRow > 0)
		{
			var baseNo = (Math.ceil((this.PageNo+1)/this.showNum)-1)*this.showNum; //first page no of current showNum pages
			var pages = Math.ceil(this.recentListCount / this.PageRow);
			if(pages > 0 && baseNo + pages <= this.PageNo)
				this.PageNo = baseNo + pages - 1;
		}		
	}
	this.show();
};

function TGrid_headTR()
{
	var td = "";
	var len = 0;
	if(this.listheader)
		len = this.listheader.length;
	for(var j = 0;j < len;j++)
	{
		var h = this.listheader[j];
		td += "<td>"+h+"</td>";		
	}
	return "<tr>"+td+"</tr>";
};

function TGrid_extendGrid(rowId)
{
	var lastId = $("#currentBotsID").attr("value");
	var row = document.getElementById("r"+rowId);
	if(row == null)
		return;
	$("#currentBotsID").attr("value",rowId);	
	if(lastId == 0) {		
		$("#parkchatnone").hide(10);
		$("#parkchat").show(10);
	}
};

function TGrid_show()
{
	var parent = document.getElementById(this.parentID);
	if(parent == null)
		return;
		
	var tr = this.headTR();
	if(this.PageRow*1 < 0) 
		var from = 0;
	else
		var from = this.PageNo * this.PageRow;
	
	for(var i = from;i < this.recentListCount;i++)
	{
		if(this.PageRow >=0 && i >= (from + this.PageRow))
			break;
		var td = "";
		var row = this.listData[i];
		if(row == null)
			continue;
		var rowlen = row.length;		
		opt = "onclick = 'gbllist[\""+this.listName+"\"].extendGrid("+row[0]+")' style='cursor:pointer'";
		if(this.parentID == "parkbotslist") {
			td += "<td><img src=\""+row[1]+"\" "+opt+"/></td>";
			for(var j = 2;j < rowlen;j++)
			{
				td += "<td>"+row[j]+"</td>";
			}			
		} else {
			td += "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+row+"</td>";
		}
		tr += "<tr>"+td+"</tr>";
		tr += "<tr><td colspan = "+(rowlen-1)+"><div id=\"r"+row[0]+"\" style=\"display:none\"><table></table></div><input type=\"hidden\" id=\"v"+row[0]+"\" value=0></td></tr>";		
	}
	if(this.addPageToolBar)
		tr += this.getPageToolBar();
	var tbl = "<table cellspacing=0 cellpadding=0 border=0 algin=\"center\" width=\"100%\">"+tr+"</table>";
	parent.innerHTML = tbl;	
};

function TGrid_getImg(src,opt)
{
	var now = new Date();
    var timeMark = "" + now.getHours() + "" + now.getMinutes() + "" + now.getSeconds();
	var option = "src=\""+src+"?" + timeMark + "\"";
	var str = "<img "+option+"/>" 
	return str;
};